<?php

namespace app\api\controller;

use DOMDocument;
use DOMXPath;

class Zreadhtml extends Ztest
{
    public function read(){
        // 读取HTML文件内容
        //$url = "https://www.yiwugo.com/product/detail/932170999.html";
        $url22 = "http://www.toywxmall.com/myscript/932170999.html";
        $html = file_get_contents($url22);
        $html22 = str_replace("?x-oss-process=image/resize,w_70,h_70", "", $html);
        //$html33 = str_replace("/\?x-oss-process=image\/resize,w_70,h_70/", "", $html22);
        $html33 = str_replace('src="//', 'src="https://', $html22);
        //echo $html33;


        $doc = new DOMDocument();
        $doc->loadHTML($html33);
        $xpath = new DOMXPath($doc);

        // 输出html内容 $doc->saveHTML($element);
        $elements = $xpath->query("//div[contains(concat('', normalize-space(@class), ''), 'swiper-container')]");
        foreach ($elements as $element) {
            echo $doc->saveHTML($element);
        }

        // 输入标签中的属性值，class一定是上级层级，<div class="swiper-wrapper"><img/></div>
//        $elements = $xpath->query("//div[contains(concat('', normalize-space(@class), ''), 'swiper-wrapper')]/img/@src");
//        foreach ($elements as $element) {
//            echo $element->nodeValue . "<br>";
//        }
    }
}
